<?php

class ComtransController extends Controller {

    public function actionIndex() {
        $this->pageTitle = Yii::t('app', 'Manager Transport company');

        $model = new Transporter();
        $criteriaData = new CDbCriteria();
        $criteriaData->addCondition('del_flg = 0');

        $dataProvider = new CActiveDataProvider('Transporter', array(
            'pagination'=>array(
                'pageSize'=>SystemConfig::PAGE_SIZE,
            ),
            //fillter order by created and status
            'criteria'=> $criteriaData,
        ));

        $this->render('index', array(
            'model' => $model,
            'dataProvider'=>$dataProvider,
        ));
    }

    public function actionAdd() {
        $this->pageTitle = Yii::t('app', 'Manager Transport company');

        $model = new Transporter();

        if (isset($_POST['Transporter']) && $data = $_POST['Transporter']) {
            $model->attributes = $data;
            if ($model->save()) {
                Yii::app()->user->setFlash('success', Yii::t('app', 'Transport company created success!'));
                $this->redirect(array('comtrans/index'));
            }
        }

        $this->render('add', array('model' => $model));
    }

    public function actionUpdate($id = null) {
        $this->pageTitle = Yii::t('app', 'Manager Transport company');
        if (!$id) {
            Yii::app()->user->setFlash('error', Yii::t('app', 'Missing parameters!'));
            $this->redirect(Yii::app()->request->urlReferrer);
        }

        //Get store type
        $model = Transporter::model()->findByPk($id, 'del_flg = 0');

        if (!$model) {
            Yii::app()->user->setFlash('error', Yii::t('app', 'Invalid record!'));
            $this->redirect(Yii::app()->request->urlReferrer);
        }

        if (isset($_POST['Transporter']) && $data = $_POST['Transporter']) {
            $model->attributes = $data;
            if ($model->save()) {
                Yii::app()->user->setFlash('success', Yii::t('app', 'Transport company updated success!'));
                $this->redirect(array('comtrans/index'));
            }
        }

        $this->render('update', array('model' => $model));
    }

    public function actionDelete($id = null) {
        if (!$id) {
            Yii::app()->user->setFlash('error', Yii::t('app', 'Missing parameters!'));
            $this->redirect(Yii::app()->request->urlReferrer);
        }

        //Get Transporter by id
        $model = Transporter::model()->findByPk($id, 'del_flg = 0');

        if (!$model) {
            Yii::app()->user->setFlash('error', Yii::t('app', 'Invalid record!'));
            $this->redirect(Yii::app()->request->urlReferrer);
        }

        $transaction = Yii::app()->db->beginTransaction();
        try{
            $model->del_flg = 1;
            if ($model->save(false)) {
                //delete all transporter detail and task
                $criteriaTD = new CDbCriteria();
                $criteriaTD->addCondition('id_transporter = :id_transporter');
                $criteriaTD->addCondition('del_flg = :del_flg');
                $criteriaTD->params[':id_transporter'] = $id;
                $criteriaTD->params[':del_flg'] = 0;
                $tranDetailModel = TransporterDetail::model()->findAll($criteriaTD);
                $arrtranDetail = array();
                if($tranDetailModel){
                    foreach($tranDetailModel as $tranDetail){
                        $arrtranDetail[] = $tranDetail->id_tpdetail;
                        $tranDetail->del_flg = 1;
                        $tranDetail->save(false);
                    }
                }

                $criteriaTask = new CDbCriteria();
                $criteriaTask->addInCondition('id_tpdetail', $arrtranDetail);
                $criteriaTask->addCondition('del_flg = :del_flg');
                $criteriaTask->params[':del_flg'] = 0;
                $taskModel = Task::model()->findAll($criteriaTask);
                if($taskModel){
                    foreach($taskModel as $task){
                        $task->del_flg = 1;
                        $task->save(false);
                    }
                }
                $transaction->commit();
                if(!isset($_GET['ajax'])){
                    Yii::app()->user->setFlash('success', Yii::t('app', 'Transport company - Deleted Successfully'));
                    $this->redirect(array('comtrans/index'));
                }
                else{
                    echo '<div class="alert alert-success fade in"><button type="button" class="close" data-dismiss="alert">×</button>'.Yii::t('app', 'Transport company - Deleted Successfully').'</div>';
                    die();
                }

            }
        }catch (Exception $ex) {
            $transaction->rollback();
            echo $ex->getMessage();
        }

    }
}